<?php

class Size extends CI_Controller {
    
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function view($id){
        $data['data']['parcat']= $this->msize->getByProId($id);
        $data['data']['pro_id']= $id;
        $data['title'] = "Kích thước";
        $data['temp'] = "backend/size/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function add($id) {
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[price_bonus]', 'Giá thêm', 'trim|required|integer');
            $this->form_validation->set_rules('data[size]', 'Kích thước', 'trim|required');

            if ($this->form_validation->run() == TRUE) {
                $post['pro_id'] = $id;
                $this->msize->insert($post);
                redirect("backend/size/view" . "/" . $id);
            }
        }

        $data['data']['pro_id'] = $id;
        $data['title'] = "Thêm kích thước";
        $data['temp'] = "backend/size/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function update($id) {
        $data['data']['pro'] = $this->msize->fetchOne($id);
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[price_bonus]', 'Giá thêm', 'trim|required|integer');
            $this->form_validation->set_rules('data[size]', 'Kích thước', 'trim|required');

            if ($this->form_validation->run() == TRUE) {
                $this->msize->update($id,$post);
                redirect("backend/size/view" . "/" . $data['data']['pro']->pro_id);
            }
        }
        $data['data']['pro_id'] = $data['data']['pro']->pro_id;
        $data['title'] = "Sửa kích thước";
        $data['temp'] = "backend/size/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function delete($id,$pro_id){
        $this->msize->delete($id);
        redirect("backend/size/view" . "/" . $pro_id);
    }
    
    public function deleteall($id){
        $this->msize->deleteAll($id);
        redirect("backend/size/view" . "/" . $id);
    }

}
